package it.polimi.traveldream.entity;

import it.polimi.traveldream.dto.TransportDTO;

import java.io.Serializable;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * The persistent class for the transport database table.
 * 
 */
@Entity
@Table(name = "transport")
@DiscriminatorValue("transport")
@NamedQueries({
		@NamedQuery(name = Transport.FIND_ALL, query = "SELECT t FROM Transport t WHERE t.valid = true"),
		@NamedQuery(name = Transport.FIND_ORIGIN, query = "SELECT t FROM Transport t WHERE t.origin = :origin AND t.valid = true"),
		@NamedQuery(name = Transport.FIND_DESTINATION, query = "SELECT t FROM Transport t WHERE t.destination = :destination AND t.valid = true"),
		@NamedQuery(name = Transport.FIND_ORIGIN_DESTINATION, query = "SELECT t FROM Transport t WHERE t.origin = :origin AND t.destination = :destination AND t.valid = true") })
public class Transport extends Product implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String FIND_ALL = "Transport.findAll";

	public static final String FIND_ORIGIN = "Transport.findOrigin";

	public static final String FIND_DESTINATION = "Transport.findDestination";

	public static final String FIND_ORIGIN_DESTINATION = "Transport.findOriginDestination";

	public enum Type {
		FLIGHT;
	}

	@NotEmpty
	private String destination;

	@NotEmpty
	private String origin;

	@NotNull
	@Enumerated(EnumType.STRING)
	private Type type;

	public Transport() {
	}

	public Transport(TransportDTO transport) {
		super(transport);
		this.setDestination(transport.getDestination());
		this.setOrigin(transport.getOrigin());
		this.setType(transport.getType());
	}

	public String getDestination() {
		return this.destination;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public String getOrigin() {
		return this.origin;
	}

	public void setOrigin(String origin) {
		this.origin = origin;
	}

	public Type getType() {
		return this.type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public void setType(String type) {
		this.setType(Type.valueOf(type));
	}

	@Override
	public TransportDTO convertToDTO() {
		TransportDTO transport = new TransportDTO();
		transport.setDescription(this.getDescription());
		transport.setDestination(this.getDestination());
		transport.setId(this.getId());
		transport.setName(this.getName());
		transport.setOrigin(this.getOrigin());
		transport.setPrice(this.getPrice());
		transport.setType(this.getType().toString());
		transport.setValid(this.getValid());
		return transport;
	}

}